JavaScript Pocket Reference by David Flanagan
Author:David Flanagan [David Flanagan]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / JavaScript
ISBN: 9781449316617
Publisher: O'Reilly Media
Published: 2012-04-08T16:00:00+00:00
Augmenting Classes
JavaScript’s prototype-based inheritance mechanism is dynamic: an object inherits properties from its prototype, even if the properties of the prototype change after the object is created. This means that we can augment JavaScript classes simply by adding new methods to their prototype objects. Here is code that adds a method to our Range class:
// Return a new range with negated endpoints Range.prototype.negate = function() { return new Range(-this.to, -this.from); };
The prototype object of built-in JavaScript classes is also “open” like this, which means that we can add methods to numbers, strings, arrays, functions, and so on. Here are some examples:
// Invoke the function f this many times, passing the // iteration number. E.g., to print "hello" 3 times: // var n = 3; // n.times(function(n) { console.log(n + " hello"); }); Number.prototype.times = function(f, context) { var n = Number(this); for(var i = 0; i < n; i++) f.call(context, i); }; // Define the ES5 String.trim() method if it does not // exist. This method trims space from the start and end. String.prototype.trim = String.prototype.trim || function() { if (!this) return this; return this.replace(/^\s+|\s+$/g, ""); }; // Return a function's name or "". If it has a name // property, use it. Otherwise, convert the function to // a string and extract the name from that. Function.prototype.getName = function() { return this.name || this.toString().match(/function\s*([^(]*)\(/)[1]; };
It is possible to add methods to Object.prototype, making them available on all objects. This is not recommended, however, because prior to ECMAScript 5, there is no way to make these add-on methods nonenumerable, and if you add properties to Object.prototype, those properties will be reported by all for/in loops.
Download
JavaScript Pocket Reference by David Flanagan.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9911)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9335)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Svelte with Test-Driven Development by Daniel Irvine(7099)
Test-Driven Development with PHP 8 by Rainier Sarabia(6829)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6693)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6532)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Web Development with Django by Ben Shaw Saurabh Badhwar(6162)
React Application Architecture for Production by Alan Alickovic(5880)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5806)
Kotlin in Action by Dmitry Jemerov(5061)
Audition by Ryu Murakami(4583)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4419)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4315)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4270)
Functional Programming in JavaScript by Mantyla Dan(4037)
